class DictsController < ApplicationController
  before_action :set_dict, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index    
    @dicts = Dict.where(build_search(search_params)).order(created_at: :desc).page(params[:page])    
    @dict_value = DictValue.new
    
    respond_to do |format|
      format.html{}
      format.json {render json:@dicts }
    end
  end

  def show
    @dict_value = DictValue.new
    @dict.dict_values.build
    
    respond_with(@dict)
    
  end

  def new
    @dict = Dict.new
    respond_with(@dict)
  end

  def edit
  end

  def create
    @dict = Dict.new(dict_params)
    @dict.save
    respond_with(@dict)
  end

  def update
    @dict.update(dict_params)
    respond_with(@dict)
  end

  def destroy
    @dict.destroy
    respond_with(@dict)
  end

  private
    def set_dict
      @dict = Dict.find(params[:id])
    end

    def dict_params
      params.require(:dict).permit(:code, :name)
    end
    
    def search_params     
        if params[:dict].present?  
          dict_params.permit(:code,:name)
        end                     
    end
end
